package com.nf.health.app.activity.my;

import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.nf.health.app.R;
import com.nf.health.app.activity.global.BaseActivity;
import com.nf.health.app.activity.my.impl.MyContact;
import com.nf.health.app.http.Apis;
import com.nf.health.app.models.HousePersionCompeteModel;
import com.nf.health.app.utils.NetUtil;
import com.nf.health.app.utils.ViewUtil;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 健康明白人知识大赛规则
 */

public class HousePersionKnowledgeRulerActivity extends BaseActivity {
    HousePersionCompeteModel housePersionCompeteModel;
    @Bind(R.id.wb_load_hoursepersioncompete_ruler)
    WebView wb_load_hoursepersioncompete_ruler;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitleBar(R.layout.titlebar_base);
        setContent(R.layout.activity_housepersionknowledgeruler);
        ButterKnife.bind(this);
        ViewUtil.setText(this, R.id.titlebar_title_tv, "大赛规则");
        regisitListener();
        initView();
    }

    private void regisitListener() {

    }

    private void initView() {
        housePersionCompeteModel = (HousePersionCompeteModel) getIntent().getSerializableExtra(MyContact.WEB_TO_SENSD_FLAG);
        ProxyBridge pBridge = new ProxyBridge();
        wb_load_hoursepersioncompete_ruler.getSettings().setJavaScriptEnabled(true);
        WebSettings webSettings = wb_load_hoursepersioncompete_ruler.getSettings();
        //
//        webSettings.setUseWideViewPort(true);
//        webSettings.setLoadWithOverviewMode(true);

        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        webSettings.setAllowFileAccess(true);
        webSettings.setBuiltInZoomControls(true);
        webSettings.setSupportZoom(true);
        WebSettings wSet = wb_load_hoursepersioncompete_ruler.getSettings();
        wSet.setDomStorageEnabled(true);
        wSet.setDatabaseEnabled(true);
        wSet.setJavaScriptEnabled(true);
        //自适应屏幕
        //wSet.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        wSet.setLoadWithOverviewMode(true);
        wSet.setUseWideViewPort(true);
        //设置支持缩放
        wSet.setBuiltInZoomControls(true);
        //设置支持加载图片
        wSet.setBlockNetworkImage(false);
        //设置支持缓存
        wSet.setAppCacheEnabled(true);
        //设置缓存模式为默认
        wSet.setCacheMode(WebSettings.LOAD_DEFAULT);
        //设置扩大比例的缩放
        wSet.setUseWideViewPort(true);
        //设置默认的编码格式
        wSet.setDefaultTextEncodingName("UTF-8");
        //设置文件支持
        wSet.setAllowFileAccess(true);
        wb_load_hoursepersioncompete_ruler.clearCache(true);
        //对特殊版本做一些处理
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN){
            wSet.setAllowUniversalAccessFromFileURLs(true);
            wSet.setAllowFileAccessFromFileURLs(true);
        }
        //String  url = "https://www.baidu.com";
        String  url = housePersionCompeteModel.getSHintUlr();
       /* if(!Apis.HOST_COMMON.contains("test.")){
            url = Act.MYSJHOPING_STORE_ONLINE+"userid="+ PreferenceHelper.getString(PreferenceHelper.USERID,"")+"&source=3";
        }else{
            url = Act.MYSJHOPING_STORE_OFFLINE+"userid="+PreferenceHelper.getString(PreferenceHelper.USERID,"")+"&source=3";
        }*/

        if (NetUtil.isNetworkAvailable(this)) {
            showLoading();
            wb_load_hoursepersioncompete_ruler.setWebViewClient(wvc);
            wb_load_hoursepersioncompete_ruler.loadUrl(url+ Apis.WEBVIEW_PLETEFORM);//"https://www.baidu.com/"
        }else{//无网的话进行提示
            showToast(getResources().getString(R.string.l_loading_connect_err));
        }
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()){
        }
    }
    public class ProxyBridge {
        public int one () {
            return 1;
        }
    }
    // 创建WebViewClient对象
    WebViewClient wvc = new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            // 使用自己的WebView组件来响应Url加载事件，而不是使用默认浏览器器加载页面
            wb_load_hoursepersioncompete_ruler.loadUrl(url);
            // 记得消耗掉这个事件。给不知道的朋友再解释一下，Android中返回True的意思就是到此为止吧,事件就会不会冒泡传递了，我们称之为消耗掉
            return true;
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            //Toast.makeText(getApplicationContext(), "WebViewClient.onPageFinished", Toast.LENGTH_SHORT).show();
            super.onPageFinished(view, url);
            hideLoading();
        }


        @Override
        public void onLoadResource(WebView view, String url) {
            //Toast.makeText(getApplicationContext(), "WebViewClient.onLoadResource", Toast.LENGTH_SHORT).show();
            super.onLoadResource(view, url);
        }
        @Override
        public void onReceivedError(WebView view, int errorCode,String description, String failingUrl) {
            //Toast.makeText(getApplicationContext(), "WebViewClient.onReceivedError", Toast.LENGTH_SHORT).show();
            super.onReceivedError(view, errorCode, description, failingUrl);
            hideLoading();
        }
        @Override
        public void onScaleChanged(WebView view, float oldScale, float newScale) {
            //Toast.makeText(getApplicationContext(), "WebViewClient.onScaleChanged", Toast.LENGTH_SHORT).show();
            super.onScaleChanged(view, oldScale, newScale);
        }
    };
    /**
     * 设置回退 覆盖Activity类的onKeyDown(int keyCoder,KeyEvent event)方法
     *  goBack()表示返回webView的上一页面
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (wb_load_hoursepersioncompete_ruler.canGoBack()) {
                wb_load_hoursepersioncompete_ruler.goBack(); // goBack()表示返回WebView的上一页面
                return true;
            } else {
                finish();
                return true;
            }
        }
        return false;
    }
}
